package com.magist.manage.copyrights.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.CxtHolder;
import com.magist.core.entity.ResultMessage;
import com.magist.core.enums.UserTypeEnum;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.BeanCopyUtil;
import com.magist.core.util.DateUtil;
import com.magist.core.util.ExcelUtil;
import com.magist.core.util.MAssertUtil;
import com.magist.manage.copyrights.dto.SupplyInfoDto;
import com.magist.manage.copyrights.dto.SupplyLinkMansDto;
import com.magist.manage.copyrights.dto.SupplyLinkMansListDto;
import com.magist.manage.copyrights.entity.SupplyInfo;
import com.magist.manage.copyrights.entity.SupplyLinkMans;
import com.magist.manage.copyrights.export.SupplyInfoExportDto;
import com.magist.manage.copyrights.importExcel.SupplyInfoImportDto;
import com.magist.manage.copyrights.service.SupplyInfoService;
import com.magist.manage.copyrights.service.SupplyLinkMansService;
import com.magist.manage.util.AuthFilterUtil;
import com.magist.manage.web.dto.UserInfoDto;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Api(value="供应商接口",tags={"供应商接口"})
@RestController
@RequestMapping("/rest/supplyInfo")
public class RestSupplyInfoController extends BaseController {

	@Autowired
	private SupplyInfoService supplyInfoService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private SupplyLinkMansService supplyLinkMansService;

	@ApiOperation(value="保存", notes="保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("supplyInfoDto") SupplyInfoDto supplyInfoDto) {
		supplyInfoService.addSupply(supplyInfoDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(supplyInfoDto);

		return result;
	}

	@ApiOperation(value="更新", notes="更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("supplyInfoDto") SupplyInfoDto supplyInfoDto) {
		SupplyInfo oldSupplyInfo = supplyInfoService.getById(supplyInfoDto.getId());
		if(oldSupplyInfo==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;

		if (supplyInfoService.updateSupplyInfo(supplyInfoDto)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("supplyInfoDto") SupplyInfoDto supplyInfoDto) {
		ResultMessage result = ResultMessage.success();
//		SupplyInfo supplyInfo = BeanCopyUtil.copy(supplyInfoDto, SupplyInfo.class);

		Example example = new Example(SupplyInfo.class);
		example = packExample(example,supplyInfoDto);
		AuthFilterUtil.doAddAuthCondition(example,userInfoService.getById(CxtHolder.getUser().getUserId()));

//		if(!StringUtils.isEmpty(supplyInfo.getNickName())) {
//			supplyInfo.setNickName("%"+supplyInfo.getNickName()+"%");
//		}
//		UserInfo userInfo = userInfoService.getById(CxtHolder.getUser().getUserId());
//		if (UserTypeEnum.OP.getCode().equals(userInfo.getUserType())) {
//			supplyInfo.setCreateById(userInfo.getId());
//			supplyInfo.setAuthById(userInfo.getId());
//		}

		PageInfo<SupplyInfo> pageInfo = supplyInfoService.page(example,getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value="删除", notes="删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		SupplyInfo supplyInfo = supplyInfoService.getById(id);
		if(supplyInfo==null)
			throw new BusinessException("无效的数据!");
		ResultMessage result =null;
		if(supplyInfoService.delete(supplyInfo)>0)
			result = ResultMessage.success("操作成功");
		else
			result=ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value="详情", notes="详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		SupplyInfo supplyInfo = supplyInfoService.getById(id);
		if(supplyInfo==null)
			throw new BusinessException("无效的数据!");
		Example example = new Example(SupplyLinkMans.class);
		example = supplyInfoLinkManExample(example,supplyInfo.getId());
		List<SupplyLinkMans> supplyLinkMansList=supplyLinkMansService.find(example);

		SupplyInfoDto supplyInfoDto = new SupplyInfoDto();
		BeanUtils.copyProperties(supplyInfo,supplyInfoDto);
//		supplyInfoDto.setEntryDate(DateUtil.format(supplyInfo.getEntryDate(),"yyyy-MM-dd"));
		if(supplyLinkMansList!=null){
			List<SupplyLinkMansDto> dto = BeanCopyUtil.copy(supplyLinkMansList,SupplyLinkMansDto.class);
			supplyInfoDto.setSupplyLinkMans(dto);
		}
		ResultMessage result = ResultMessage.success();
		result.setContent(supplyInfoDto);
		return result;
	}

	private Example packExample(Example example,SupplyInfoDto supplyInfoDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(supplyInfoDto.getId()))
			criteria.andEqualTo("id",supplyInfoDto.getId());
		if(StringUtils.isNotEmpty(supplyInfoDto.getNickName()))
			criteria.andLike("nickName","%"+supplyInfoDto.getNickName()+"%");
		if(StringUtils.isNotEmpty(supplyInfoDto.getOrganizationType()))
			criteria.andEqualTo("organizationType",supplyInfoDto.getOrganizationType());
		if(StringUtils.isNotEmpty(supplyInfoDto.getSupplyType()))
			criteria.andEqualTo("supplyType",supplyInfoDto.getSupplyType());
		if(StringUtils.isNotEmpty(supplyInfoDto.getSupplyLevel()))
			criteria.andEqualTo("supplyLevel",supplyInfoDto.getSupplyLevel());
		if(StringUtils.isNotEmpty(supplyInfoDto.getStatus()))
			criteria.andEqualTo("status",supplyInfoDto.getStatus());
		if(StringUtils.isNotEmpty(supplyInfoDto.getPersonId()))
			criteria.andEqualTo("personId",supplyInfoDto.getPersonId());
		return example;
	}

	private Example userInfoExample(Example example,UserInfoDto userInfoDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(userInfoDto.getId()))
			criteria.andEqualTo("id",userInfoDto.getId());
		return example;
	}

	private Example supplyInfoLinkManExample(Example example,String supplyId){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(supplyId))
			criteria.andEqualTo("supplyId",supplyId);
		return example;
	}


	//公司联系人列表
	@ApiOperation(value="列表", notes="分页查询接口")
	@RequestMapping(value = "linkManList.do", method = RequestMethod.GET)
	public Object linkManList(@ModelAttribute("userInfoDto") UserInfoDto userInfoDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(UserInfo.class);
		example = userInfoExample(example,userInfoDto);
		List<UserInfo> userInfoList=userInfoService.find(example);
		result.setContent(userInfoList);
		return result;
	}

	//供应商列表
	@ApiOperation(value="供应商列表", notes="供应商列表,通过简称模糊查询")
	@PostMapping(value = "supplyInfoList.do")
	public Object supplyInfoList(@ModelAttribute("supplyInfoDto") SupplyInfoDto supplyInfoDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(SupplyInfo.class);

		example = supplyInfoList(example,supplyInfoDto);
		List<SupplyInfo> supplyInfos = supplyInfoService.find(example);
		result.setContent(supplyInfos);
		return result;
	}
	private Example supplyInfoList(Example example,SupplyInfoDto supplyInfoDto){
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted","F");
		if(StringUtils.isNotEmpty(supplyInfoDto.getNickName())) {
			Example.Criteria criteria1 = example.createCriteria();
			criteria1.andLike("nickName", "%" + supplyInfoDto.getNickName() + "%");
//			criteria1.orLike("usrName", "%" + supplyInfoDto.getNickName() + "%");
//			criteria1.orLike("companyName", "%" + supplyInfoDto.getNickName() + "%");

			example.and(criteria1);
		}

		return example;
	}


	@ApiOperation(value="保存单个联系人", notes="保存单个联系人")
	@PostMapping("saveLinkMan.do")
	public Object saveLinkMan(@ModelAttribute("supplyLinkMansDto") SupplyLinkMansDto supplyLinkMansDto) {
		MAssertUtil.notNull(supplyLinkMansDto,"联系人不能为空");
		SupplyLinkMans supplyLinkMans = new SupplyLinkMans();
		BeanUtils.copyProperties(supplyLinkMansDto,supplyLinkMans);
		supplyLinkMansService.saveOrUpdateSupplyLinkMan(supplyLinkMans);
		ResultMessage result = ResultMessage.success();
		result.setContent(supplyLinkMans);
		return result;
	}


	@ApiOperation(value="保存或更新多个联系人", notes="保存或更新多个联系人")
	@PostMapping("batchSaveLinkMans.do")
	public Object batchSaveLinkMans(@RequestBody SupplyLinkMansListDto supplyLinkMansListDtos) {
		MAssertUtil.notNull(supplyLinkMansListDtos.getSupplyLinkMans(),"联系人不能为空");
		MAssertUtil.notNull(supplyLinkMansListDtos.getSupplyId(),"供应商信息不能为空");
		supplyLinkMansService.saveOrUpdateSupplyLinkMans(supplyLinkMansListDtos.getSupplyId(),supplyLinkMansListDtos.getSupplyLinkMans());
		ResultMessage result = ResultMessage.success();
		return result;
	}

	@ApiOperation(value="删除单个联系人", notes="删除单个联系人")
	@DeleteMapping("delSupplyLinkMan.do")
	public Object delSupplyLinkMan(@ModelAttribute("supplyLinkManId") String supplyLinkManId) {
		ResultMessage result = ResultMessage.success();

		MAssertUtil.notNull(supplyLinkManId,"联系人ID不能为空");
		SupplyLinkMans supplyLinkMans = supplyLinkMansService.getById(supplyLinkManId);
		MAssertUtil.notNull(supplyLinkMans,"查询不到联系人");
		supplyLinkMansService.delete(supplyLinkMans);
		return result;
	}


	@ApiOperation(value="查询供应商联系人", notes="查询供应商联系人")
	@GetMapping("fetchSupplyLinkManList.do")
	public Object fetchSupplyLinkManList(@ModelAttribute("supplyId") String supplyId) {
		ResultMessage result = ResultMessage.success();
		if(StringUtils.isEmpty(supplyId)) {
			return result;
		}
		SupplyLinkMans supplyLinkMans = new SupplyLinkMans();
		supplyLinkMans.setSupplyId(supplyId);
		List<SupplyLinkMans> supplyLinkMansList = supplyLinkMansService.find(supplyLinkMans);
		result.setContent(supplyLinkMansList);
		return result;
	}


	@ApiOperation(value="excel导出", notes="excel导出")
	@RequestMapping(value = "exportExcel.do", method = RequestMethod.GET)
	public void exportExcel(@ModelAttribute("supplyInfoDto")SupplyInfoDto supplyInfoDto, HttpServletResponse response) {
		SupplyInfo supplyInfo = BeanCopyUtil.copy(supplyInfoDto, SupplyInfo.class);
		UserInfo userInfo = userInfoService.getById(CxtHolder.getUser().getUserId());
		if (UserTypeEnum.OP.getCode().equals(userInfo.getUserType())) {
			supplyInfo.setCreateById(userInfo.getId());
			supplyInfo.setAuthById(userInfo.getId());
		}
		PageHelper.startPage(1, Integer.MAX_VALUE-1,"SHOW_ID DESC");
		List<SupplyInfo> supplyInfos = supplyInfoService.find(supplyInfo);
		List< SupplyInfoExportDto > dtos = BeanCopyUtil.copy(supplyInfos, SupplyInfoExportDto.class);
		//导出操作
		ExcelUtil.exportExcel(dtos,"供应商列表","供应商列表",SupplyInfoExportDto.class,DateUtil.toString(new Date(),DateUtil.yyyy_MM_dd)+ "供应商列表.xls",response);
	}

	@ApiOperation(value="excel导入", notes="excel导入,模板下载路径(47.96.144.33:8080/供应商导入模板.xlsx)")
	@PostMapping("importExcel.do")
	public Object importExcel(@RequestParam(value="file") MultipartFile file){
		List<SupplyInfoImportDto> supplyInfoImportDtos = ExcelUtil.importExcel(file, 7, 1, SupplyInfoImportDto.class);
		List<SupplyInfoDto> supplyInfoDtos = BeanCopyUtil.copy(supplyInfoImportDtos, SupplyInfoDto.class);
		supplyInfoService.doBatchAdd(supplyInfoDtos);
		return ResultMessage.success();
	}

}
